//
//  LYRepaymentCarouseCell.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/25.
//

import UIKit

protocol LYRepaymentCarouseCellDelegate: AnyObject {
    func didClickRepaymentButton(data: Receiveer)
}

class LYRepaymentCarouseCell: UICollectionViewCell {
    
    weak var delegate: LYRepaymentCarouseCellDelegate? = nil
    
    var receiveer: Receiveer? = nil
    let bgImageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFill
        iv.image = UIImage(named: "repayment_bg_view")
        iv.clipsToBounds = true
        iv.isUserInteractionEnabled = true
        return iv
    }()
    
    let icon: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFill
        iv.image = UIImage(named: "tips_icon")
        iv.clipsToBounds = true
        iv.isUserInteractionEnabled = true
        return iv
    }()
    
    lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex: "#000000")
        label.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        label.adjustsFontSizeToFitWidth = true
        label.numberOfLines = 0
        return label
    }()
    
    lazy var repaymentButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setBackgroundImage(UIImage(named: "repayment_button_background"), for: .normal)
        button.addTarget(self, action: #selector(repaymentButtonAction(_:)), for: .touchUpInside)
        button.setTitleColor(UIColor(hex: "#FFFFFF"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .bold)
        button.setTitle(LYlocalizedString("Repayment"), for: .normal)
        button.isUserInteractionEnabled = false
        return button
    }()
    
    lazy var repaymentTitleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex: "#FFFFFF")
        label.font = UIFont.systemFont(ofSize: 16, weight: .bold)
        label.adjustsFontSizeToFitWidth = true
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(bgImageView)
        contentView.addSubview(icon)
        contentView.addSubview(contentLabel)
        contentView.addSubview(repaymentButton)
        contentView.addSubview(repaymentTitleLabel)
        setLayoutSubviews()
    }
    
    func setLayoutSubviews() {
        bgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        icon.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(10)
            make.centerY.equalToSuperview()
        }
        
        repaymentButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().offset(-16)
            make.width.equalTo(118)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.leading.equalTo(icon.snp.trailing).offset(5)
            make.top.bottom.equalToSuperview().inset(5)
            make.trailing.equalTo(repaymentButton.snp.leading).offset(-5)
        }
    }
    
    @objc func repaymentButtonAction(_ button: UIButton) {
        guard let receiveer = receiveer else { return }
        self.delegate?.didClickRepaymentButton(data: receiveer)
    }
    
    func setData(data: Receiveer) {
        self.receiveer = data
        contentLabel.text = data.motherling
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

